﻿using Authentication.Configuration;
using Authentication.Model.ViewModel.SaleOut;
using Common.Contract;
using Common.Contract.Incentive;
using Common.Contract.Incentive.Request;
using Common.Contract.SaleOut;
using Common.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using Common.Type;
using Common.Service.DataTables;
using Common.Contract.SaleOut.Request;
using Common.Contract.Paging;
using Common.Contract.SaleOut.Record;
using Common.Contract.Incentive.Record;

namespace Authentication.Web.Controller
{
    public class IncentiveController : ControllerBase
    {
        #region SchemeBalance actions
        public ActionResult SchemeBalance(string fromDate, string toDate)
        {
            return View();
        }
        [HttpPost]
        public ActionResult SchemeBalance(SellOutDailyViewModel vm, FormCollection form)
        {
            try
            {
                if (form["btnSearch"] != null)
                {
                    return RedirectToAction("SchemeBalance", "incentive", new { FromDate = vm.FromDate.ToString("MM/dd/yyyy"), ToDate = vm.ToDate.ToString("MM/dd/yyyy") });
                }
            }
            catch (Exception ex)
            {
                vm.MessageType = MessageType.Error;
                vm.Message = ex.ToString();
            }

            return View();
        }

        [HttpPost]
        public ActionResult SchemeBalanceData(DataTablesPageRequest pageRequest, string fromDate, string toDate, string dropBillShop)
        {
            DateTime date = DateTime.Now;
            var firsDayOfMonth = new DateTime(date.Year, date.Month, 1);
            DataTableResponse pageResponse = null;
            using (ServiceClient<IIncentive> serviceClient = new ServiceClient<IIncentive>())
            {
                RetrieveSchemeBalanceRequest request = new RetrieveSchemeBalanceRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "Authentication.Web.Controller.IncentiveController.Index"
                    },
                    Page = new PageRecord
                    {
                        PageSize = pageRequest.DisplayLength,
                        PageIndex = pageRequest.DisplayLength > 0 ? pageRequest.DisplayStart / pageRequest.DisplayLength : 0
                    },
                    DropBillShop = dropBillShop.Trim(),
                    FromDate = string.IsNullOrEmpty(fromDate) ? firsDayOfMonth.ToString("MM/dd/yyyy") : fromDate,
                    ToDate = string.IsNullOrEmpty(toDate) ? firsDayOfMonth.AddMonths(1).AddDays(-1).ToString("MM/dd/yyyy") : toDate
                };
                var schemeBalanceIncentiveRecords = serviceClient.Proxy.RetrieveBalanceIncentive(request);
                var resultDailyReport = (schemeBalanceIncentiveRecords == null || schemeBalanceIncentiveRecords.Records == null) ? new List<SchemeBalanceRecord>() : schemeBalanceIncentiveRecords.Records;
                pageResponse = new DataTableResponse
                {
                    sEcho = pageRequest.Echo,
                    iTotalRecords = schemeBalanceIncentiveRecords.Page.TotalRecords,
                    sColumns = pageRequest.ColumnNames,
                    aaData = resultDailyReport.Select(x => new object[]{
                                                             x.Region, 
                                                             x.Province,
                                                             x.CustName,
                                                             x.Address,
                                                             x.BillTo,
                                                             x.BillName,
                                                             x.Product,
                                                             x.Model,
                                                             x.Qty,
                                                             x.WtAmt,
                                                             x.InctAmt,
                                                             x.Scheme
                    }).ToList()
                };
            }
            return Json(pageResponse);
        }
        #endregion

        #region Incentive Closed Actions
        public ActionResult IncentiveClosed()
        {
            return View();
        }
        [HttpPost]
        public ActionResult IncentiveClosed(SellOutDailyViewModel vm, FormCollection form)
        {
            try
            {
                if (form["btnSearch"] != null)
                {
                    return RedirectToAction("SchemeBalance", "incentive", new { FromDate = vm.FromDate.ToString("MM/dd/yyyy"), ToDate = vm.ToDate.ToString("MM/dd/yyyy") });
                }
            }
            catch (Exception ex)
            {
                vm.MessageType = MessageType.Error;
                vm.Message = ex.ToString();
            }

            return View();
        }

        [HttpPost]
        public ActionResult IncentiveClosedData(DataTablesPageRequest pageRequest, string fromDate, string toDate, string dropOption)
        {
            DateTime date = DateTime.Now;
            var firsDayOfMonth = new DateTime(date.Year, date.Month, 1);
            DataTableResponse pageResponse = null;
            using (ServiceClient<IIncentive> serviceClient = new ServiceClient<IIncentive>())
            {
                RetrieveIncentiveClosedRequest request = new RetrieveIncentiveClosedRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "Authentication.Web.Controller.IncentiveController.Index"
                    },
                    Page = new PageRecord
                    {
                        PageSize = pageRequest.DisplayLength,
                        PageIndex = pageRequest.DisplayLength > 0 ? pageRequest.DisplayStart / pageRequest.DisplayLength : 0
                    },
                    DropOption = dropOption.Trim(),
                    FromDate = string.IsNullOrEmpty(fromDate) ? firsDayOfMonth.ToString("MM/dd/yyyy") : fromDate,
                    ToDate = string.IsNullOrEmpty(toDate) ? firsDayOfMonth.AddMonths(1).AddDays(-1).ToString("MM/dd/yyyy") : toDate
                };
                var incentiveClosedRecords = serviceClient.Proxy.RetrieveIncentiveClosed(request);
                var resultDailyReport = (incentiveClosedRecords == null || incentiveClosedRecords.Records == null) ? new List<IncentiveClosedRecord>() : incentiveClosedRecords.Records;
                pageResponse = new DataTableResponse
                {
                    sEcho = pageRequest.Echo,
                    iTotalRecords = incentiveClosedRecords.Page.TotalRecords,
                    sColumns = pageRequest.ColumnNames
                };
                if (dropOption.Equals("Summary"))
                {
                    pageResponse.aaData = resultDailyReport.Select(x => new object[]{
                                                             x.Region, 
                                                             x.Province,
                                                             x.ShopCode,
                                                             x.ShopName,
                                                             x.Address,
                                                             x.BillCode,
                                                             x.BillName,
                                                             x.Qty,
                                                             x.InctAmt,
                                                             x.Scheme
                    }).ToList();
                }
                else if (dropOption.Equals("Detail"))
                {
                    pageResponse.aaData = resultDailyReport.Select(x => new object[]{
                                                             x.Region, 
                                                             x.Province,
                                                             x.ShopCode,
                                                             x.ShopName,
                                                             x.Address,
                                                             x.BillCode,
                                                             x.BillName,
                                                             x.Product,
                                                             x.Model,
                                                             x.Qty,
                                                             x.WtAmt,
                                                             x.InctAmt,
                                                             x.Scheme
                    }).ToList();
                }
                else
                {
                    pageResponse.aaData = resultDailyReport.Select(x => new object[]{
                                                             x.Region, 
                                                             x.Province,
                                                             x.ShopCode,
                                                             x.ShopName,
                                                             x.BillCode,
                                                             x.BillName,
                                                             x.Product,
                                                             x.Model,
                                                             x.SerialNo,
                                                             x.InctAmt,
                                                             x.MsgBody,
                                                             x.Scheme
                    }).ToList();
                }
            }
            return Json(pageResponse);
        }
        #endregion

        #region Scheme Mgr Actions
        public ActionResult SchemeMgr(string fromDate, string toDate)
        {
            return View();
        }
        [HttpPost]
        public ActionResult SchemeMgr(SellOutDailyViewModel vm, FormCollection form)
        {
            try
            {
                if (form["btnSearch"] != null)
                {
                    return RedirectToAction("SchemeMgr", "incentive", new { FromDate = vm.FromDate.ToString("MM/dd/yyyy"), ToDate = vm.ToDate.ToString("MM/dd/yyyy") });
                }
            }
            catch (Exception ex)
            {
                vm.MessageType = MessageType.Error;
                vm.Message = ex.ToString();
            }

            return View();
        }

        [HttpPost]
        public ActionResult SchemeMgrData(DataTablesPageRequest pageRequest, string fromDate, string toDate)
        {
            DateTime date = DateTime.Now;
            var firsDayOfMonth = new DateTime(date.Year, date.Month, 1);
            DataTableResponse pageResponse = null;
            using (ServiceClient<IIncentive> serviceClient = new ServiceClient<IIncentive>())
            {
                RetrieveSchemeMgrRequest request = new RetrieveSchemeMgrRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "Authentication.Web.Controller.IncentiveController.Index"
                    },
                    Page = new PageRecord
                    {
                        PageSize = pageRequest.DisplayLength,
                        PageIndex = pageRequest.DisplayLength > 0 ? pageRequest.DisplayStart / pageRequest.DisplayLength : 0
                    },
                    TextSearch = pageRequest.Search.Trim(),
                    FromDate = string.IsNullOrEmpty(fromDate) ? firsDayOfMonth.ToString("MM/dd/yyyy") : fromDate,
                    ToDate = string.IsNullOrEmpty(toDate) ? firsDayOfMonth.AddMonths(1).AddDays(-1).ToString("MM/dd/yyyy") : toDate
                };
                var schemeMgrRecords = serviceClient.Proxy.RetrieveSchemeMgr(request);
                var resultDailyReport = (schemeMgrRecords == null || schemeMgrRecords.Records == null) ? new List<SchemeMgrRecord>() : schemeMgrRecords.Records;
                pageResponse = new DataTableResponse
                {
                    sEcho = pageRequest.Echo,
                    iTotalRecords = schemeMgrRecords.Page.TotalRecords,
                    sColumns = pageRequest.ColumnNames,
                    aaData = resultDailyReport.Select(x => new object[]{
                                                             x.Region, 
                                                             x.Product,
                                                             x.Point,
                                                             x.Model,
                                                             x.Amt
                    }).ToList()
                };
            }
            return Json(pageResponse);
        }
        #endregion

    }
}
